package com.specialtreasure.freightdepot.adapter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.app.core.config.Constants;
import com.app.core.config.HttpApi;
import com.app.core.util.CommonUtil;
import com.app.core.util.UserUtils;
import com.app.core.util.DateUtils;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.dialog.SureDialogFragment;
import com.specialtreasure.freightdepot.model.BaseModel;
import com.specialtreasure.freightdepot.model.InformationBean;
import com.yanzhenjie.httpmodule.http.CallServer;
import com.yanzhenjie.httpmodule.http.Result;
import com.yanzhenjie.httpmodule.http.StringRequest;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.CacheMode;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/7 0007.
 * 信息管理适配器
 */

public class InformationAdapter extends BaseAdapter  {
    List<InformationBean> informations = new ArrayList<InformationBean>();
    int type;
    FragmentActivity context;
    Integer itemId;


    public InformationAdapter(FragmentActivity context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if (informations != null && informations.size() > 0) {
            return informations.size();
        }

        return 0;
    }

    @Override
    public Object getItem(int i) {
        return informations.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (null == view) {
            view = View.inflate(viewGroup.getContext(), R.layout.item_information_list, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //判断状态
        if (type == 1) {
            viewHolder.tv_car_status.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_car_status.setVisibility(View.GONE);
        }


        final InformationBean informationBean = (InformationBean) getItem(i);
        if (informationBean != null) {
            viewHolder.tv_address.setText(CommonUtil.checkString(informationBean.getStartCityAddress()) + " -> "
                    + CommonUtil.checkString(informationBean.getDestCityAddress()));
            viewHolder.tv_type.setText(CommonUtil.checkString(informationBean.getContent()));
            viewHolder.tv_time.setText(DateUtils.timeMinute(informationBean.getPushTime()));
//            Logger.e("time:"+informationBean.getPushTime());



            //确定
            viewHolder.tv_car_status.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SureDialogFragment sureDialogFragment = SureDialogFragment.newInstance(informationBean.getItemId());
                    sureDialogFragment.show(context.getSupportFragmentManager(), "");

                }
            });


            //删除
            viewHolder.tv_car_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new AlertDialog.Builder(context)
                            .setTitle("删除信息")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int m) {
                                    if (informations != null && informations.size() > 0) {
                                        informations.remove(i);
                                        notifyDataSetChanged();
                                        itemId=informationBean.getItemId();
                                        delItem();

                                    }

                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int m) {
                                    dialogInterface.dismiss();
                                }
                            })
                            .show();
                }
            });


        }



        return view;
    }


    public static class ViewHolder {
        public View rootView;
        public TextView tv_address;
        public TextView tv_month;
        public TextView tv_time;
        public TextView tv_type;
        public TextView tv_car_status;
        public TextView tv_car_del;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_address = (TextView) rootView.findViewById(R.id.tv_address);
            this.tv_month = (TextView) rootView.findViewById(R.id.tv_month);
            this.tv_time = (TextView) rootView.findViewById(R.id.tv_time);
            this.tv_type = (TextView) rootView.findViewById(R.id.tv_type);
            this.tv_car_status = (TextView) rootView.findViewById(R.id.tv_car_status);
            this.tv_car_del = (TextView) rootView.findViewById(R.id.tv_car_del);
        }

    }

    /**
     * 删除信息
     */
    private void delItem(){
        String url = Constants.HTTP_URL + HttpApi.DELITEM;
        StringRequest request = new StringRequest(url, RequestMethod.POST);
        request.add("token", UserUtils.getToken());
        request.add("itemId", itemId);

        request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);


        CallServer.getInstance().request(Constants.ZERO, request, new SimpleResponseListener<Result<String>>() {
            @Override
            public void onSucceed(int what, Response<Result<String>> response) {
                super.onSucceed(what, response);
                String result = response.get().getResult();
                Logger.e("请求成功：" + result);
                BaseModel baseModel = JSON.parseObject(result, BaseModel.class);
                if (baseModel != null) {
                    Integer status = baseModel.getStatus();
                    String msg = baseModel.getMsg();
                    if (status != null && status == 0) {
                        Toast.makeText(context, "删除信息成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                    }
                }


            }
        });


    }




    public void setInformations(List<InformationBean> informations) {
        this.informations = informations;
    }

    public void setType(int type) {
        this.type = type;
    }
}
